﻿using Simulation.Framework;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
 

/// <summary>
/// 統一查找子物體
/// </summary>
public class UniFiedFindChild
{
    /// <summary>
    /// 查找子物体（递归查找）  
    /// </summary> 
    /// <param name="trans">父物体</param>
    /// <param name="goName">子物体的名称</param>
    /// <returns>找到的相应子物体</returns>
    public static Transform FindChild(Transform trans, string goName)
    {
        Transform child = trans.Find(goName);
        if (child != null)
            return child;
 
        Transform go = null;
        for (int i = 0; i < trans.childCount; i++)
        {
            child = trans.GetChild(i);
            go = FindChild(child, goName);
            if (go != null)
                return go;
        }
        return null;
    }
 
    /// <summary>
    /// 查找子物体（递归查找）  where T : UnityEngine.Object
    /// </summary> 
    /// <param name="trans">父物体</param>
    /// <param name="goName">子物体的名称</param>
    /// <returns>找到的相应子物体</returns>
    public static T FindChild<T>(Transform trans, string goName) where  T : Object
    {
        Transform child = trans.Find(goName);
        if (child != null)
        {
            return child.GetComponent<T>();
        }
 
        Transform go = null;
        for (int i = 0; i < trans.childCount; i++)
        {
            child = trans.GetChild(i);
            go = FindChild(child, goName);
            if (go != null)
            {
                return go.GetComponent<T>();
            }
        }
        return null;
    }
    /// <summary>
    /// 获得所有的子物体
    /// </summary>
    /// <param name="trans">父物体</param>
    /// <returns></returns>
    public static Transform[] GetChilds(Transform trans)
    {
        Transform[] transforms = new Transform[trans.childCount];
        for (int i=0;i<trans.childCount;i++)
        {
            transforms[i] = trans.GetChild(i);
        }
        
        return transforms;
    }
}
/// <summary>
/// 数据处理
/// </summary>
public class UniData
{
    /// <summary>
    /// 去掉字符串中的数字
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string RemoveNumber(string key)
    {
        return System.Text.RegularExpressions.Regex.Replace(key, @"\d", "");
    }



    /// <summary>
    /// 去掉字符串中的非数字
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string RemoveNotNumber(string key)
    {
        return System.Text.RegularExpressions.Regex.Replace(key, @"[^\d]*", "");
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="filePath"></param>
    public static void DelectFile(string filePath)
    {
 
        bool result = File.Exists(filePath);
        if (result)
        {
            
            File.Delete(filePath);
           
        }
        else
        {
           
        }
    }

    /// <summary>
    /// 删除文件夹下所有的文件
    /// </summary>
    /// <param name="srcPath"></param>
    public static void DelectDir(string srcPath)
    {
        try
        {
            DirectoryInfo dir = new DirectoryInfo(srcPath);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)            //判断是否文件夹
                {
                    DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                    subdir.Delete(true);          //删除子目录和文件
                }
                else
                {
                    File.Delete(i.FullName);      //删除指定文件
                }
            }
        }
        catch (System.Exception e)
        {
            throw;
        }
    }
}
/// <summary>
/// Unity方法拓展
/// </summary>
static public class UniFiedsMethodExtensionForUnity
{
    // <summary>
    /// Gets or add a component. Usage example:
    /// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();
    /// </summary>
    static public T GetOrAddComponent<T>(this Component child, bool set_enable = false) where T : Component
    {
        T result = child.GetComponent<T>();
        if (result == null)
        {
            result = child.gameObject.AddComponent<T>();
        }
        var bcomp = result as Behaviour;
        if (set_enable)
        {
            if (bcomp != null) bcomp.enabled = true;
        }
        return result;
    }
 
    static public T GetOrAddComponent<T>(this GameObject go) where T : Component
    {
        T result = go.transform.GetComponent<T>();
        if (result == null)
        {
            result = go.AddComponent<T>();
        }
        var bcomp = result as Behaviour;
        if (bcomp != null) bcomp.enabled = true;
        return result;
    }
 
}

